package cn.hx.controller;

import cn.hx.entity.User;
import cn.hx.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

    @Autowired
    private UserService userService;

    @GetMapping({"login","/"})
    public String getLogin(){
        return "login";
    }
    @GetMapping("main")
    public String getMain(){
        return "main";
    }

//    登录校验
    @PostMapping("userLogin")
    public String postUserLogin(User user, HttpSession session, Model model){
        User users = userService.selectByUser(user);

        if (users!=null){
//            管理员用户0
            if (user.getRoleId()==0){
                session.setAttribute("loginUser",users);
                return "index";
            }else {
//            普通用户1
                session.setAttribute("loginUser",users);
                return "userIndex";
            }
        }
        model.addAttribute("info","信息错误请重新输入");
        return "login";
    }

//    注销登录
    @GetMapping("logout")
    public String getLoginUt(HttpSession session){
        session.invalidate();
        return "redirect:/login";
    }

}
